<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@include file="/common/taglibs.jsp"%>
<%@include file="/common/meta.jsp"%>
<title>选择角色</title>
</head>
<body>

	<div style="padding-bottom: 10px; text-align: center;">
		当前用户：<c:out value="${user.userName }"/>
		<input id="submit" type="button" value="保存"/>
	</div>
	<div id="chooseRoleGrid"></div>
	
	<script type="text/javascript">
		$(function() {
			$("#chooseRoleGrid").flexigrid({
				url: "<c:url value='/pf/role/dataGrid'/>",
				colModel: [
					{display: "选择", name: "", process: _chooses},
					{display: "角色名称", name: "roleName"},
					{display: "备注", name: "memo"},
					{display: "是否有效", name: "roleIsvalid"},
					{display: "", name: "id", hide: true}
				],
				height: 350
			});
			
			$("#submit").click(function() {
				var temp = "";
				$("input[id ^= 'role']:checked").each(function() {
					temp += $(this).val() + ",";
				});
				
				if("" == temp || temp.length < 0) {
					art.dialog.alert("请选择角色进行保存!");
					return false;
				}
				
				$.post(
					"<c:url value='/pf/user/saveChooseRole'/>",
					{"userId": "${user.id }", "roleIds": temp.substring(0, temp.length - 1)},
					function(data, status) {
						if("success" == status) {
							parent.closeAndRefresh();
						} else {
							art.dialog.alert(data.msg);
						}
					}		
				);
			});
		});
		
		//选择
		function _chooses(tdDiv, id) {
			$(tdDiv).height("15px");
			var temp = "${roleIds}";
			if(temp.indexOf(id) != -1) {
				$(tdDiv).html("<input id='role" + id + "' checked='true' type='checkbox' value='" + id + "'/>");
			} else {
				$(tdDiv).html("<input id='role" + id + "' type='checkbox' value='" + id + "'/>");
			}
		}
	</script>
</body>
</html>